package com.soulkun.smarthome.ui.main.personal.dialog

import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppDialogMainPersonalFamilyMemberDeleteBinding
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalFamilySettingViewModel
import soulkun.library.mvvm.component.BaseDataBindingDialogFragment
import soulkun.library.utils.toPx
import soulkun.library.utils.toast

class AppMainPersonalFamilyMemberDeleteDialog(private val viewModel: AppMainPersonalFamilySettingViewModel) :
    BaseDataBindingDialogFragment<AppDialogMainPersonalFamilyMemberDeleteBinding>() {

    override fun setLayout(): Int {
        return R.layout.app_dialog_main_personal_family_member_delete
    }

    override fun doOnViewCreated() {
        requireDialog().window?.run {
            setLayout(requireContext().resources.getDimensionPixelSize(R.dimen.sw_300dp), ViewGroup.LayoutParams.WRAP_CONTENT)
        }
        isCancelable = false
        requireDataBinding().onClickListener = View.OnClickListener {
            when (it.id) {
                R.id.tv_main_personal_family_delete_member_text_cancel -> {
                    dismissAllowingStateLoss()
                }
                R.id.tv_main_personal_family_delete_member_text_determine -> {
                    viewModel.deleteMember().observe(viewLifecycleOwner) { result ->
                        result.getOrNull()?.run {
                            if (result.isSuccess) {
                                statusMessage.toast()
                                viewModel.familyMemberList.remove(viewModel.selectFamilyUserLiveData.value)
                                dismissAllowingStateLoss()
                            }
                        }
                    }
                }
            }
        }
    }

    override fun doOnResume() {
    }

    override fun doOnDismiss() {
    }

    override fun doOnDestroyView() {
    }

}